package com.sunday.electric.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.electric.R;
import com.sunday.electric.base.BaseActivity;
import com.sunday.electric.event.LoginOut;
import com.sunday.electric.http.ApiClient;
import com.sunday.electric.ui.LoginActivity;
import com.sunday.electric.ui.MainActivity;
import com.sunday.electric.ui.ShowBigImage;
import com.sunday.electric.ui.XWalkExUiClient;
import com.sunday.electric.utils.SharePerferenceUtils;

import org.xwalk.core.JavascriptInterface;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkView;
import org.xwalk.core.XWalkWebResourceRequest;
import org.xwalk.core.XWalkWebResourceResponse;

import java.net.URLEncoder;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/1/14.
 */
public class DetailViewActivity extends BaseActivity {
    @Bind(R.id.webView)
    XWalkView mXWalkView;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    String url = "http://202.110.130.19:1080/RedAS/app/appPage.go?method=goUcDetailPage";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);
        url = !TextUtils.isEmpty(getIntent().getStringExtra("url"))?getIntent().getStringExtra("url"):url;
        mXWalkView.load(url, null);
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        mXWalkView.setUIClient(new XWalkExUiClient(mXWalkView));
        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
        mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
        //   mXWalkView.load("javascript:document.body.contentEditable=true;", null);
        mXWalkView.setResourceClient(new XWalkResourceClient(mXWalkView) {

            @Override
            public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                super.onReceivedLoadError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onLoadFinished(XWalkView view, String url) {
                emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                super.onLoadFinished(view, url);
            }

            @Override
            public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
                return super.shouldInterceptLoadRequest(view, request);
            }

            @Override
            public void onLoadStarted(XWalkView view, String url) {
                super.onLoadStarted(view, url);
            }
        });
    }

    public void sign(String planId) {
        String latitude = SharePerferenceUtils.getIns().getString(Constants.LATITUDE, "120");
        String longitude = SharePerferenceUtils.getIns().getString(Constants.LONGITUDE, "30");
        String address = URLEncoder.encode(SharePerferenceUtils.getIns().getString(Constants.LOCATION_ADDRESS, "无具体签到位置"));
        String method = "updateJcPlanExcuteExcStatusToExcute"; //工作负责人签到方法

        Call<ResultDO> call = ApiClient.getIMAdapter().siginUser(latitude, longitude,
                method, planId, address);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                ResultDO resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    ToastUtils.showToast(mContext, "签到成功");
                /*    intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);
                    SharePerferenceUtils.getIns().putBoolean(Constants.IS_LOGIN, true);
                    finish();*/
                } else {
                    ToastUtils.showToast(mContext, "签到失败");
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "签到异常");
            }
        });
    }

    public class JsInterface {
        public JsInterface() {
        }

        //签到接口 参数为计划id
        @JavascriptInterface
        public void sign(String planId) {
            DetailViewActivity.this.sign(planId);
        }

/*        //到岗到位领导签到接口 参数计划Id
        @JavascriptInterface
        public void signLeader(String planId) {
            ((MainActivity) getActivity()).signLeader(planId);
        }*/


        //返回键
        @JavascriptInterface
        public void doReturn() {
            DetailViewActivity.this.finish();
        }


        //与相应人员聊天
        @JavascriptInterface
        public void clickChatPerson(String loginName) {
            intent = new Intent(mContext, OnLineHelpActivity.class);
            intent.putExtra("loginName", loginName);
            startActivity(intent);
        }



        //产看图片接口
        @JavascriptInterface
        public void showImage(String path) {
            if (TextUtils.isEmpty(path)) {
                return;
            }
      /*      byte[]bitmapArray;
            bitmapArray=Base64.decode(path, Base64.DEFAULT);*/
            //     String tempPath = writeImageToDisk(bitmapArray);
            // String tempPath = ImageUtils.saveBitmap(stringtoBitmap(path));
            // String tempPath  = StringUtils.getRandomString(32)+".jpg";
            // File file = new File("/sdcard",tempPath);
            // decoderBase64File(path,file.getPath());
            String tempPath = path;
            intent = new Intent(mContext, ShowBigImage.class);
            intent.putExtra("imgPath", tempPath);
            startActivity(intent);
        }
    }
}
